﻿package com.pandemicode.framework.utils {
	import flash.geom.Point;
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class MathEx {
		/**
		 * An extension of the Math class that has some more useful functions.
		 */
		public function MathEx() { }
		
		/**
		 * Converts radians to degrees.
		 * @param	radians	The radians to convert.
		 * @return	Returns radians to degrees.
		 */
		public static function radiansToDegrees(radians:Number):Number {
			return radians * 360 / (2 * Math.PI);
		}
		
		/**
		 * Converts degrees to radians.
		 * @param	degrees	The degrees to convert.
		 * @return	Returns degrees to radians.
		 */
		public static function degreesToRadians(degrees:Number):Number {
			return degrees * 2 * Math.PI / 360;
		}
		
		/**
		 * Returns a point that defines the direction of an angle.
		 * @param	degrees	The degrees to calculate the direction of.
		 * @return	Returns a point that defines the direction of an angle.
		 */
		public static function direction(degrees:Number):Point {
			var r:Number = degreesToRadians(degrees);
			return new Point(Math.cos(r), Math.sin(r));
		}
		
		/**
		 * Returns the distance between 2 points.
		 * @param	p0	The first point.
		 * @param	p1	The second point.
		 * @return	Returns the distance between 2 points.
		 */
		public static function distance(p0:Point, p1:Point):Number {
			var d:Point = new Point(p1.x - p0.x, p1.y - p0.y);
			return Math.sqrt(d.x * d.x + d.y * d.y);
		}
		
		/**
		 * Rounds off a number.
		 * @param	n	The number to round.
		 * @param	d	The number of decimals.
		 * @return	Rounds off a number.
		 */
		public static function round(n:Number, d:Number):Number {
			return Math.round(n * d) / d;
		}
		
		/**
		 * Returns the average of the numbers passed.
		 * @param	...params	A series of numbers to take the average of.
		 * @return	Returns the average of the numbers passed.
		 */
		public static function average(...params):Number {
			var numbers:Array = params.filter(Filter.isNumber);
			return sum(numbers) / numbers.length;
		}
		
		/**
		 * Returns the sum of the numbers passed.
		 * @param	...params	The numbers to calculate the sum of.
		 * @return	Returns the sum of the numbers passed.
		 */
		public static function sum(...params):Number {
			var n:Number = 0;
			for each(var num:Number in params.filter(Filter.isNumber))
				n += num;
			return n;
		}
	}
}